package com.kaili.controller.web.v1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.exception.BaseExcept;
import com.kaili.common.util.FreemarkUtils;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.db.service.IHsEwbsSignDetailService;
import com.kaili.db.service.IHsEwbsSignService;
import com.kaili.db.service.IHsOptArriveService;
import com.kaili.except.ResponseStatus;
import com.kaili.feign.UserFeignClient;
import com.kaili.pojo.dto.sign.SignAddDTO;
import com.kaili.pojo.dto.sign.SignQueryDTO;
import com.kaili.pojo.vo.ArriveEwbVO;
import com.kaili.pojo.vo.SignPrintVO;
import com.kaili.pojo.vo.SignQueryVO;
import com.kaili.version.ApiVersion;
import freemarker.template.Configuration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "签收服务", description = "签收服务API")
@RestController
@ApiVersion(1)
@Validated
@RequestMapping("/{api_version}")
public class SignController {
    @Autowired
    private IHsEwbsSignDetailService signDetailService;
    @Autowired
    private IHsEwbsSignService signService;
    @Autowired
    private IHsOptArriveService hsOptArriveService;
    @Autowired
    private Configuration configuration;
    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/signs")
    @ApiOperation(value = "获取签收/未签收数据列表 pickMode 区分自提(106)或者派送(105)")
    public Page<SignQueryVO> getSignQueryList(@Validated @ModelAttribute SignQueryDTO req) throws BaseExcept {
        if(req.getCur()!=null&&req.getPageSize()!=null){
            Page<SignQueryVO> pager = new Page<>(req.getCur(), req.getPageSize());
            Map<Object, Object> search = new HashMap<>();
            search.putAll(new org.apache.commons.beanutils.BeanMap(req));
            pager = signDetailService.getSignList(pager, search);
            return pager;
        }
        throw new BaseExcept(ResponseStatus.SIGN_SEARCH_ERR);
    }

    @PostMapping("/signs")
    @ApiOperation(value = "新增签收")
    public ResponseStatus addSign(@Validated @RequestBody SignAddDTO req) throws BaseExcept {
        int ret = signService.createSignDateByBS(req);
        if (ret != 0) {
            return ResponseStatus.SIGN_ADD_ERR;
        }
        return ResponseStatus.RESPONSE_OK;
    }

    @GetMapping("/signs/html")
    @ApiOperation(value = "获取自提信息打印")
    public String queryPrintLiftEwb(@RequestParam("ewbNo") @ApiParam(value="运单号逗号连接字符串",defaultValue = "0000011111") String ewbNo, @RequestParam("flag") @ApiParam(value="1自提0签收") Integer flag) throws Exception {
        if(StringUtils.isEmpty(ewbNo)){
            throw new BaseExcept(ResponseStatus.PRINT_NULL);
        }
        String logo = userFeignClient.getCompanyLogo(Long.parseLong(LoginDetailUtils.getCompanyCode()));
        JSONObject json= JSON.parseObject(logo);
        List<String> ewbList = Arrays.asList(ewbNo.split(","));
        Map parm = new HashMap();
        parm.put("ewbList",ewbList);

        List<ArriveEwbVO> list = hsOptArriveService.queryPrintArriveEwb(parm);
        List<SignPrintVO> signPrintVOS = new ArrayList<>();
        SignPrintVO signPrintVO = new SignPrintVO();
        signPrintVO.setSignMan(list.get(0).getSignMan());
        signPrintVO.setActualCharge(list.get(0).getActualCharge());
        signPrintVO.setShouldCharge(list.get(0).getShouldCharge());
        signPrintVO.setList(list);
        signPrintVOS.add(signPrintVO);

        Map<String, Object> map = new HashMap<>();
        map.put("list",signPrintVOS);
        map.put("companyName", LoginDetailUtils.getCompanyName());
        if(StringUtils.isEmpty(String.valueOf(json.get("data")))){
            map.put("pictureUrl","../api/static/logo.png");
        }else {
            map.put("pictureUrl", json.get("data"));
        }
        if (flag == 1) {
            map.put("title", "自提");
        }else {
            map.put("title", "签收");
        }
        return FreemarkUtils.getPrintTemplate( configuration, "liftPrint", map);
    }
}
